package com.Final.hj;

import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

public class StudenSystem extends AppCompatActivity implements View.OnClickListener {
    private static final String STATE_FRAGMENT_SHOW = "exit_fragment";
    //菜单按钮
    private ImageView student_menu;
    //侧边栏
    private DrawerLayout student_drawer_menu;
    //下拉刷新
    private NavigationView student_nav_view;
    private RecyclerView student_recyclerView;

    //两个frange
    private StudentCreate studentCreate;
    private StudentJoin studentJoin;
    private FragmentManager fragmentManager;
    private Fragment currentFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.studen_system);
        initView();
        //创建 FragmentManager实例
        fragmentManager = getSupportFragmentManager();

//        instanceState(savedInstanceState);
    }
    
    public void initView(){
        student_menu = (ImageView) findViewById(R.id.student_menu);
        student_drawer_menu = (DrawerLayout) findViewById(R.id.student_drawerLayout_wrap);
        student_nav_view = (NavigationView) findViewById(R.id.student_nav_view);
        student_recyclerView = (RecyclerView) findViewById(R.id.student_recyclerView);

        //RecyclerView
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(StudenSystem.this);
        student_recyclerView.setLayoutManager(linearLayoutManager);

        //设置默认第一项选中
        student_nav_view.setCheckedItem(R.id.studen_create_class);
        //为点击事件绑定this
        student_menu.setOnClickListener(this);

        //侧边栏点击
        student_nav_view.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(MenuItem menuItem) {
                Toast.makeText(StudenSystem.this, menuItem.getTitle(), Toast.LENGTH_SHORT).show();
//                student_drawer_menu.closeDrawers();
                return true;
            }
        });
        
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        //“内存重启”时保存当前的fragment名字
        outState.putString(STATE_FRAGMENT_SHOW, currentFragment.getClass().getName());
        super.onSaveInstanceState(outState);
    }

    //fragment的切换
    public void showFragment(Fragment fg, int itemId){

        FragmentTransaction transaction = fragmentManager.beginTransaction();//创建一个事务
        if(!fg.isAdded()){//没有添加过
            transaction
                    .hide(currentFragment)//第三个参数为当前的fragment绑定一个tag，tag为当前绑定fragment的类名
                    .add(R.id.rl_content, fg, fg.getClass().getName());
        }else{
            transaction
                    .hide(currentFragment)
                    .show(fg);//添加过了直接显示即可
        }

        //记录当前fragment
        currentFragment = fg;
        transaction.commit();//事务一定要提交，replace才会有效
    }
    //解决内存重启问题
    public void instanceState(Bundle savedInstanceState){
        // “内存重启”时调用
        if (savedInstanceState != null) {
            Toast.makeText(StudenSystem.this, "有", Toast.LENGTH_SHORT).show();
            //获取“内存重启”时保存的fragment名字
            String saveFragmentName = savedInstanceState.getString(STATE_FRAGMENT_SHOW);
            //从fragmentManager里面找到fragment
            studentCreate = (StudentCreate) fragmentManager.findFragmentByTag(StudentCreate.class.getName());
            studentJoin = (StudentJoin) fragmentManager.findFragmentByTag(StudentJoin.class.getName());

            //说明不是在fragment内存重启的
            if(TextUtils.isEmpty(saveFragmentName)){
                //解决重叠问题show里面可以指定恢复的页面
                fragmentManager.beginTransaction()
                        .show(studentCreate)
                        .hide(studentJoin)
                        .commit();

                //把当前显示的fragment记录下来
                currentFragment = studentCreate;
            }else{
                showExitFragment(saveFragmentName);
            }
        }else{
            //正常启动时调用 没有内存重启
            studentCreate = new StudentCreate();
            studentJoin = new StudentJoin();
            //默认显示第一个
            currentFragment = new EmptyFragment();
            showFragment(studentCreate, 0);
        }
    }

    public void showExitFragment(String saveFragmentName){
        if(saveFragmentName.equals(studentCreate.getClass().getName())){
            //解决重叠问题
            fragmentManager.beginTransaction()
                    .show(studentCreate)
                    .hide(studentJoin)
                    .commit();

            //把当前显示的fragment记录下来
            currentFragment = studentCreate;

        }else if(saveFragmentName.equals(studentJoin.getClass().getName())){  //如果推出之前是TwoFragment

            //解决重叠问题
            fragmentManager.beginTransaction()
                    .show(studentJoin)
                    .hide(studentCreate)
                    .commit();

            //把当前显示的fragment记录下来
            currentFragment = studentJoin;

        }
    }
    
    
    //重写点击事件
    @Override
    public void onClick(View v){
        switch (v.getId()){
            case R.id.student_menu:
                //显示侧边栏
                student_drawer_menu.openDrawer(Gravity.START);
                break;
        }
    }
}
